﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.Domain;
using MShop.Core.Extensions ;
using MShop.Core.DataAccess;
using MShop.Core;
 

namespace MShop.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class WorkSpaceController : MShop.Web.Areas.Admin.Controllers.SecureController
    {
        private readonly IMenuDao _MenuDao;
        private readonly IShopContext _ShopContext;
        private readonly ICommonDao<WorkSpace> _WorkSpaceDao;
        public WorkSpaceController(IMenuDao menuDao,IShopContext shopContext ,ICommonDao<WorkSpace> workSpaceDao)
        {
            _ShopContext = shopContext;
            _MenuDao = menuDao;
            _WorkSpaceDao = workSpaceDao;
        }

        public ActionResult Index()
        {
            IList<Menu> menus = _MenuDao.Find(x => x.ParentId == null).ToList();

            int[] menuids = _WorkSpaceDao.Find(x => x.UserId == _ShopContext.CurrentUser.UserId).Select(x => x.MenuId).ToArray();
                       
            ViewData["menus"] = menus;
            ViewData["menuids"] = menuids;
            //有争议
            ViewData["workspaces2"] = _WorkSpaceDao.Find(x => x.UserId == _ShopContext.CurrentUser.UserId).OrderBy(x => x.Sort);
            return View();
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            var workspaces = _WorkSpaceDao.Find(x => x.UserId == _ShopContext.CurrentUser.UserId).ToList();

           // int[] menuids = workspaces.Select(x => x.MenuId).ToArray();


            foreach (var item in workspaces)
            {
                _WorkSpaceDao.Delete(item);
            }

            _WorkSpaceDao.SaveChanges();
            int count = 1;

            if (!string.IsNullOrEmpty(collection["sel2"]))
            {
                if (collection["sel2"].IndexOf(',') == -1)
                {
                        WorkSpace workspace  = new WorkSpace();
                        workspace.MenuId = int.Parse(collection["sel2"]);
                        workspace.UserId = _ShopContext.CurrentUser.UserId;
                        workspace.Sort = count;
                        _WorkSpaceDao.Save(workspace);
                   
                }
                else
                {

                    collection["sel2"].Split(',').ToList()
                        .ForEach((x) =>
                        {
                            int menuid = int.Parse(x);
                            WorkSpace workspace = null;
                            //if (menuids.Contains(menuid))
                            //{                       
                            //    workspace = DataContext.WorkSpace.First(y => y.MenuId == menuid && y.UserId == CurrentUser.UserId);
                            //    workspace.Sort = count;
                            //}
                            //else
                            {
                                workspace = new WorkSpace();
                                workspace.MenuId = menuid;
                                workspace.UserId = _ShopContext.CurrentUser.UserId;
                                workspace.Sort = count;
                                _WorkSpaceDao.Save(workspace);
                            }
                            count++;
                        });
                }

                _WorkSpaceDao.SaveChanges();
            }
            return RedirectToAction("index");
        }
    }
}
